package db;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{sendLoginForm(response, false);}
    private void sendLoginForm(HttpServletResponse response, boolean withErrorMessage)
        throws ServletException, IOException{

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("<TITLE>Login</TITLE>");
        out.println("</HEAD>");
        out.println("<BODY>");
        out.println("<CENTER>");

        if (withErrorMessage)
            out.println("Введите логин и пароль вашей учетной записи");
        out.println("<BR>");
        out.println("<BR> <FORM METHOD=POST>");
        out.println("<TABLE>");

        out.println("<TR>");
        out.println("<TD>Login</TD>");
        out.println("<TD><INPUT TYPE=TEXT NAME=name></TD>");
        out.println("</TR>");

        out.println("<TR>");
        out.println("<TD>Password</TD>");
        out.println("<TD><INPUT TYPE=PASSWORD NAME=password></TD>");
        out.println("</TR>");

        out.println("<TR>");
        out.println("<TD ALIGN=RIGHT COLSPAN=2>");
        out.println("<INPUT TYPE=SUBMIT VALUE=Login></TD>");
        out.println("</TR>");

        out.println("</TABLE>");
        out.println("</FORM>");
        out.println("</CENTER>");
        out.println("</BODY>");
        out.println("</HTML>");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        if (login(name, password)){
            RequestDispatcher rd = request.getRequestDispatcher("AnotherServlet");
            rd.forward(request, response);
        }
        else {
            sendLoginForm(response, true);
        }
    }

    boolean login (String name, String password){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/java", "root", "123456");
            System.out.println("соединение установлено");
            java.sql.Statement s = con.createStatement();
            String sql = "SELECT name, surname FROM users" + "WHERE Login='" + name + "'" + "AND Password='" + password + "'";
            ResultSet rs = s.executeQuery(sql);
            if (rs.next()){
                rs.close();
                s.close();
                con.close();
                return true;
            }
            rs.close();
            s.close();
            con.close();
        }
        catch (ClassNotFoundException e){
            System.out.println(e.toString());
        }
        catch (SQLException e){
            System.out.println(e.toString());
        }
        catch (Exception e){
            System.out.println(e.toString());
        }
        return false;
    }
}
